/**
 * @author Diego de Blas Mateo
 */

function include(filename)
{
	/*
    var htmlHead = document.getElementsByTagName('head').item(0);
    var script = document.createElement('script');

    //script.setAttribute('language', 'javascript');
    script.setAttribute('type', 'text/javascript');
    script.setAttribute('src', filename);
    
    //gets the first script
    var scripts = htmlHead.getElementsByTagName('script');
    //If previously existed at least one script section
    if (scripts.length > 0)
    	//it puts the new script in the first place
    	htmlHead.insertBefore(script, scripts[0]);
    else
    	//in other case it puts the first script section at the end
    	htmlHead.appendChild(script);

    return false;
    */
}

/**
 * Bomberman's static class
 */
Bomberman = {};

/**
 * Bomberman's static properties
 */
Bomberman.canvasArea = null;
Bomberman.drawingContext = null;
Bomberman.screenWidth = null;
Bomberman.screenHeight = null;
Bomberman.debugString = "";
Bomberman.exit = false;
Bomberman.players = new Array();
Bomberman.currentPlayer = null;


/**
 * Initializes the Game
 * 
 */
Bomberman.Initialize = function (params)
{
	//We create the game only if we receive the basic parameters
	if (params !== undefined && typeof(params) == "object")
	{
		var string = "";
		
		if (params.display !== undefined)
		{	
			Bomberman.canvasArea = document.getElementById(params.display);
			Bomberman.drawingContext = Bomberman.canvasArea.getContext("2d");
			Bomberman.screenWidth = this.canvasArea.clientWidth;
			Bomberman.screenHeight = this.canvasArea.clientHeight;
			Bomberman.debugString += "Display area: " + params.display + " - Width: " + this.canvasArea.clientWidth + " - Height: " + this.canvasArea.clientHeight;
		}
		else throw "The 'display' parameter must be passed to the constructor";
		
		if (params.players !== undefined)
		{
			//include("scripts/game/bomberman/player.js");
			
			Bomberman.debugString += "\nNumber of players: " + params.players;
		}
		else throw "The 'players' parameter must be passed to the constructor";
		
		//Attaches events
		window.addEventListener("keydown", Bomberman.keyboardHandler, false);
	}
}

Bomberman.keyboardHandler = function (event)
{
	event.preventDefault();
	
	//Left
	if (event.keyCode == 37)
	{
		Bomberman.currentPlayer.moveLeft();
	}
	//Up
	else if (event.keyCode == 38)
	{
		Bomberman.currentPlayer.moveUp();
	}
	//Right
	else if (event.keyCode == 39)
	{
		Bomberman.currentPlayer.moveRight();
	}
	//Down
	else if (event.keyCode == 40)
	{
		Bomberman.currentPlayer.moveDown();
	}
	
	if(Bomberman.currentPlayer)
		Bomberman.players[Bomberman.currentPlayer.id] = Bomberman.currentPlayer;
	
	Bomberman.Paint();
}

Bomberman.RegisterPlayer = function (player, currentPlayer)
{
	if(currentPlayer) Bomberman.currentPlayer = player;
	Bomberman.players.push(player);
	return player;
}

Bomberman.UnregisterPlayer = function (playerId)
{
	for (var i = 0; i < Bomberman.players.length; i++)
	{
		if (Bomberman.players[i].id == playerId)
		{
			Bomberman.players.slice(i, 1);
			return true;
		}
	}
	//If the players wasn't found, we will notify that event
	return false;
}


Bomberman.getPlayer = function(id)
{
	for(var i in Bomberman.players)
	{
		if(Bomberman.players[i].id == id)
			return Bomberman.players[i];
	}
	
	//return Bomberman.players[id];
}

Bomberman.setPlayerPosition = function(id, X, Y)
{
	var player = this.getPlayer(id);

	player.setPosition(X,Y);
	
	if(id == Bomberman.currentPlayer.id)
		Bomberman.currentPlayer = player;
}

Bomberman.Paint = function ()
{
	//Clears the context
	Bomberman.drawingContext.clearRect(0, 0, Bomberman.screenWidth, Bomberman.screenHeight);
	//Draws all the players
	for (var i in Bomberman.players)
	{
		Bomberman.players[i].paint();
	}
}

/**
 * Executes the game
 * @this Bomberman
 */
Bomberman.Run = function ()
{
	//alert(Bomberman.debugString);
	
	Bomberman.exit = false;
	
	//Begins the main loop
	//while (!Bomberman.exit)
	{
		Bomberman.Paint();
	}
}
